1
|
|
|
// Encoding documentation: |
2
|
|
|
// https://en.wikipedia.org/wiki/Universal_Product_Code#Encoding |
3
|
|
|
|
4
|
|
|
import encode from './encoder'; |
5
|
|
|
import Barcode from "../Barcode.js"; |
6
|
|
|
|
7
|
|
|
class UPC extends Barcode{ |
8
|
|
|
constructor(data, options){ |
9
|
|
|
// Add checksum if it does not exist |
10
|
|
|
if(data.search(/^[0-9]{11}$/) !== -1){ |
11
|
|
|
data += checksum(data); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
super(data, options); |
15
|
|
|
|
16
|
|
|
this.displayValue = options.displayValue; |
17
|
|
|
|
18
|
|
|
// Make sure the font is not bigger than the space between the guard bars |
19
|
|
|
if(options.fontSize > options.width * 10){ |
20
|
|
|
this.fontSize = options.width * 10; |
21
|
|
|
} |
22
|
|
|
else{ |
23
|
|
|
this.fontSize = options.fontSize; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Make the guard bars go down half the way of the text |
27
|
|
|
this.guardHeight = options.height + this.fontSize / 2 + options.textMargin; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
valid(){ |
31
|
|
|
return this.data.search(/^[0-9]{12}$/) !== -1 && |
32
|
|
|
this.data[11] == checksum(this.data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
encode(){ |
36
|
|
|
if(this.options.flat){ |
37
|
|
|
return this.flatEncoding(); |
38
|
|
|
} |
39
|
|
|
else{ |
40
|
|
|
return this.guardedEncoding(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
flatEncoding(){ |
45
|
|
|
var result = ""; |
46
|
|
|
|
47
|
|
|
result += "101"; |
48
|
|
|
result += encode(this.data.substr(0, 6), "LLLLLL"); |
49
|
|
|
result += "01010"; |
50
|
|
|
result += encode(this.data.substr(6, 6), "RRRRRR"); |
51
|
|
|
result += "101"; |
52
|
|
|
|
53
|
|
|
return { |
54
|
|
|
data: result, |
55
|
|
|
text: this.text |
56
|
|
|
}; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
guardedEncoding(){ |
60
|
|
|
var result = []; |
61
|
|
|
|
62
|
|
|
// Add the first digit |
63
|
|
|
if(this.displayValue){ |
64
|
|
|
result.push({ |
65
|
|
|
data: "00000000", |
66
|
|
|
text: this.text.substr(0, 1), |
67
|
|
|
options: {textAlign: "left", fontSize: this.fontSize} |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Add the guard bars |
72
|
|
|
result.push({ |
73
|
|
|
data: "101" + encode(this.data[0], "L"), |
74
|
|
|
options: {height: this.guardHeight} |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
// Add the left side |
78
|
|
|
result.push({ |
79
|
|
|
data: encode(this.data.substr(1, 5), "LLLLL"), |
80
|
|
|
text: this.text.substr(1, 5), |
81
|
|
|
options: {fontSize: this.fontSize} |
82
|
|
|
}); |
83
|
|
|
|
84
|
|
|
// Add the middle bits |
85
|
|
|
result.push({ |
86
|
|
|
data: "01010", |
87
|
|
|
options: {height: this.guardHeight} |
88
|
|
|
}); |
89
|
|
|
|
90
|
|
|
// Add the right side |
91
|
|
|
result.push({ |
92
|
|
|
data: encode(this.data.substr(6, 5), "RRRRR"), |
93
|
|
|
text: this.text.substr(6, 5), |
94
|
|
|
options: {fontSize: this.fontSize} |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
// Add the end bits |
98
|
|
|
result.push({ |
99
|
|
|
data: encode(this.data[11], "R") + "101", |
100
|
|
|
options: {height: this.guardHeight} |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
// Add the last digit |
104
|
|
|
if(this.displayValue){ |
105
|
|
|
result.push({ |
106
|
|
|
data: "00000000", |
107
|
|
|
text: this.text.substr(11, 1), |
108
|
|
|
options: {textAlign: "right", fontSize: this.fontSize} |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return result; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// Calulate the checksum digit |
117
|
|
|
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit |
118
|
|
|
export function checksum(number){ |
119
|
|
|
var result = 0; |
120
|
|
|
|
121
|
|
|
var i; |
122
|
|
|
for(i = 1; i < 11; i += 2){ |
123
|
|
|
result += parseInt(number[i]); |
124
|
|
|
} |
125
|
|
|
for(i = 0; i < 11; i += 2){ |
126
|
|
|
result += parseInt(number[i]) * 3; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return (10 - (result % 10)) % 10; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
export default UPC; |
133
|
|
|
|